<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<title>vue2.x</title>
</head>

<body>
	<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
	<script src="https://cdn.jsdelivr.net/npm/axios@0.12.0/dist/axios.min.js"></script>
	<script src="https://cdn.jsdelivr.net/npm/lodash@4.13.1/lodash.min.js"></script>

	<div id="app">
		<h2>Vue的值绑定</h2>
		<!-- 和wpf很像,default类似wpf的one-way，
					v-once类似wpf的one-time,
					v-model则是wpf的two-way
					v-html是html输出 -->
		<span>
			<input v-model="message">
			<br />
			<span>default---{{ message }}</span>
			<br />
			<span v-once>v-once---{{ message }}</span>
			<br />
			<span>{{ rawHtml }}---</span>
			<span v-html="rawHtml">></span>
		</span>
		<br />
		<!-- 除了值，也可以绑定js -->
		<span>js---number + 1---{{ number + 1 }}</span>
		<br />
		<span>js---ok ? 'YES' : 'NO'---{{ ok ? 'YES' : 'NO' }}</span>
		<br />
		<span>js---reverse---{{ message.split('').reverse().join('') }}</span>
		<br />
		</span>
		<hr />

		<h2>Vue的指令</h2>
		<span>
			<span>v-bind绑定dom元素的属性</span>
			<button v-bind:id="buttonId">button:id={{ buttonId }}</button>
			<a v-bind:href="linkBaidu">a:href=百度</a>
			<!-- 注意true/false在dom上的不同 -->
			<button v-bind:disabled="buttonDisabled">button:disabled={{ buttonDisabled }}</button>
			<hr />

			<span>v-for实现循环</span>
			<ol>
				<li v-for="todo in todos">
					{{ todo.text }}
				</li>
			</ol>
			<button v-on:click="pushTodoItem">push todo</button>
			<hr />

			<span>v-if操作Dom元素</span>
			<br />
			<span v-if="visiablity">Now you see me</span>		
			<span v-else>No you dont</span>
			<button type="button" @click="visiablityChange(true)">显示</button> <!-- @是v-on简写 -->
			<button type="button" @click="visiablityChange(false)">隐藏</button> <!-- @是v-on简写 -->
		</span>
		<hr />

		<span>
			<h2>侦听器watch</h2>
			<p>
				Ask a yes/no question:<input v-model="question">
			</p>
			<p>{{ answer }}</p>
		</span>
		<hr/>
	</div>
	<script>
		/*
			1.vue借鉴了wpf的mvvm设计，以数据驱动显示
			2.vm（viewmodel）在vue中是Vue对象的实例，包括了数据、方法
			3.数据的更改（INotifyChanged）隐式实现，可以不关注
			4.一个简单的理解是在vue下编写的dom是虚拟的，经过vue的计算才是真实的
			5.基于4，在vue实例创建之外的属性定义，不会被Vue响应（故必须提前进行声明）
			6.类似的针对这种定义，同样提供了Getter、Setter，默认只有getter；vue中这种被称之为计算属性
		*/
		var app = new Vue({
			el: '#app',
			data: {
				message: 'Hello World!',
				rawHtml: "<span style='color:red'>This is Red</span>",
				number: 2,
				ok: true,
				buttonId: '1010',
				buttonDisabled: false,
				linkBaidu: 'http://www.baidu.com',
				todos: [{ text: new Date().toLocaleString() }, { text: new Date().toLocaleString() }],
				visiablity: true,
				question: '',
				answer: 'I cannot give you an answer until you ask a question!'
			},
			methods: {
				pushTodoItem: function () {
					app.$data.todos.push({
						text: new Date().toLocaleString()
					})
				},
				visiablityChange: function (visiablity) {
					app.$data.visiablity = visiablity;
				},
				getAnswer: function () {
					if (this.question.indexOf('?') === -1) {
						this.answer = 'Questions usually contain a question mark. ;-)'
						return
					}
					this.answer = 'Thinking...'
					var vm = this
					axios.get('https://yesno.wtf/api')
						.then(function (response) {
							vm.answer = _.capitalize(response.data.answer)
						})
						.catch(function (error) {
							vm.answer = 'Error! Could not reach the API. ' + error
						})
				}
			},
			created: function () {
				// `_.debounce` 是一个通过 Lodash 限制操作频率的函数
				this.debouncedGetAnswer = _.debounce(this.getAnswer, 500)
			},
			watch: {
				// 如果 `question` 发生改变，这个函数就会运行
				question: function (newQuestion, oldQuestion) {
					this.answer = 'Waiting for you to stop typing...'
					this.debouncedGetAnswer()
				}
			}
		})
	</script>
</body>

</html>